package com.linghu.view;

import com.linghu.domain.*;
import com.linghu.service.BillService;
import com.linghu.service.DiningTableService;
import com.linghu.service.EmployeeService;
import com.linghu.service.MenuService;
import com.linghu.utils.Utility;

import java.util.List;
import java.util.Scanner;

/**
 * @author 令狐荣豪
 * @version 1.0
 * 这是我们的主界面
 */
public class MHLView {
    private boolean loop = true;
    private String key;
    Scanner scanner = new Scanner(System.in);

    //定义EmployeeServices属性
    private EmployeeService employeeService = new EmployeeService();

    //定义DiningTable的属性
    private DiningTableService diningTableService = new DiningTableService();
    private MenuService menuService = new MenuService();
    //定义BillService属性
    private BillService billService = new BillService();

    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    //完成结账
    public void payBill() {
        System.out.println("==========结账服务==============");
        System.out.print("请选择要结账的餐桌编号(-1退出)：");
        int diningTableId = Utility.readInt();
        if (diningTableId == -1) {
            System.out.println("取消结账服务");
            return;
        }
        //验证餐桌是否存在
        DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);
        if (diningTable == null) {
            System.out.println("==========结账的餐桌不存在==============");
            return;
        }
        //验证餐桌是否有需要结账的菜单
        if (!(billService.hasPayBillByDiningTableId(diningTableId))) {
            System.out.println("==========该餐位没有未结账的账单==============");
            return;
        }
        System.out.print("结账方式(现金/支付宝/微信)【回车表示退出】:");
        String payMode = Utility.readString(20, "");//说明回车，就返回""
        if ("".equals(payMode)) {
            System.out.println("==========取消结账==============");
            return;
        }

        char key=Utility.readConfirmSelection();
        if (key=='Y'){
            //调用我们写的方法
            if(billService.payBill(diningTableId,payMode)){
                System.out.println("==========结账成功==============");
            }else {
                System.out.println("==========结账失败==============");
            }
        }else {
            System.out.println("==========取消结账==============");
            return;
        }

    }

    //显示账单信息
    public void listBill() {
//        List<Bill> Bills = billService.list();
//        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t\t\t日期\t\t\t\t\t\t\t状态");
//        for (Bill bill : Bills) {
//            System.out.println(bill);//显示的是tostring方法
//        }
//        System.out.println("显示完毕！");

        //多表查询
        List<MultiTableBean> multiTableBeans = billService.list2();
        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t\t\t日期\t\t\t\t\t\t\t状态\t\t菜品名");
        for (MultiTableBean bill : multiTableBeans) {
            System.out.println(bill);//显示的是tostring方法
        }
        System.out.println("显示完毕！");

    }

    //完成点餐
    public void orderMenu() {
        System.out.println("==========点餐服务==============");
        System.out.println("请输入点餐的桌号：(-1退出)");
        int orderDiningTableId = Utility.readInt();
        if (orderDiningTableId == -1) {
            System.out.println("==========取消点餐==============");
            return;
        }
        System.out.println("请输入点餐的菜品号(-1退出)：");
        int orderMenuId = Utility.readInt();
        if (orderMenuId == -1) {
            System.out.println("==========取消点餐==============");
            return;
        }
        System.out.println("请输入点餐的菜品量(-1退出)：");
        int orderNums = Utility.readInt();
        if (orderNums == -1) {
            System.out.println("==========取消点餐==============");
            return;
        }
        //验证餐桌号是否存在
        DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
        if (diningTable == null) {
            System.out.println("==========餐桌号不存在==============");
            return;
        }
        //验证菜品编号是否正确
        Menu menu = menuService.getMenuById(orderMenuId);
        if (menu == null) {
            System.out.println("==========菜品号不存在==============");
            return;
        }

        //点餐，校验
        if (billService.orderMenu(orderMenuId, orderNums, orderDiningTableId)) {
            System.out.println("点餐成功");
        } else {
            System.out.println("点餐失败");
        }

    }

    //显示所有菜品
    public void listMenu() {
        List<Menu> list = menuService.list();
        System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t价格");
        for (Menu menu : list) {
            System.out.println(menu);
        }
        System.out.println("==========显示完毕==============");

    }


    //完成订座
    public void orderDiningTable() {
        System.out.println("==========预定餐桌==============");
        System.out.println("请选择预定的餐桌号:(-1退出):");
        int orderId = Utility.readInt();
        if (orderId == -1) {
            System.out.println("==========取消预定餐桌！==============");
            return;
        } else {
            System.out.println("确认是否预定？(Y/N)");
            String key = scanner.next();
            if ("Y".equals(key)) {//要预定
                //根据orderId 返回 对应DiningTable对象，如果为null，说明该对象不存在！
                DiningTable diningTable = diningTableService.getDiningTableById(orderId);
                if (diningTable == null) {
                    System.out.println("==========预定餐桌不存在！==============");
                    return;
                }
                //判断餐桌状态是否为空
                if (!("空".equals(diningTable.getState()))) {
                    System.out.println("==========预定餐桌已经被预定！==============");
                    return;
                }

                System.out.println("预定人的名字：");
                String orderName = Utility.readString(50);
                System.out.println("预定人的电话：");
                String orderTel = Utility.readString(50);
                //这时说明餐桌可以被预定，更新餐桌预定状态
                if (diningTableService.orderDiningTable(orderId, orderName, orderTel)) {
                    System.out.println("================预定餐桌成功！=============");
                } else {
                    System.out.println("===============预定餐桌失败!==============");
                }
            } else {
                System.out.println("==========取消预定餐桌！==============");
            }
        }
    }

    //显示所有餐桌状态
    public void listDiningTable() {
        List<DiningTable> list = diningTableService.list();
        System.out.println("\n餐桌编号\t\t餐桌状态");
        for (DiningTable diningTable : list) {
            System.out.println(diningTable);
        }
        System.out.println("==========显示完毕==============");

    }

    //显示主菜单
    public void mainMenu() {

        while (loop) {
            System.out.println("==========满汉楼==============");
            System.out.println("\t\t1、登录满汉楼");
            System.out.println("\t\t2、退出满汉楼");
            System.out.println("请输入你的选择(1-2):");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.println("请输入员工号:");
                    String empId = Utility.readString(50);
                    System.out.println("请输入密  码:");
                    String pwd = Utility.readString(50);
                    //到数据库去判断
                    Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);

                    if (employee != null) {//说明该用户存在
                        System.out.println("========【" + employee.getName() + "】登录成功！=========\n");
                        //显示二级菜单
                        while (loop) {
                            System.out.println("==========满汉楼二级菜单==============");
                            System.out.println("\t\t1、显示餐桌状态");
                            System.out.println("\t\t2、预定餐桌");
                            System.out.println("\t\t3、显示所有菜品");
                            System.out.println("\t\t4、点餐服务");
                            System.out.println("\t\t5、查看账单");
                            System.out.println("\t\t6、结账");
                            System.out.println("\t\t9、退出满汉楼");
                            System.out.println("请输入你的选择(1-9):");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    listDiningTable();
                                    break;
                                case "2":
                                    orderDiningTable();
                                    break;
                                case "3":
                                    listMenu();
                                    break;
                                case "4":
                                    orderMenu();
                                    break;
                                case "5":
                                    listBill();
                                    break;
                                case "6":
                                    payBill();
                                    break;
                                case "9":
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("你的输入有误，请重新输入~");
                            }
                        }
                    } else {
                        System.out.println("========登录失败！=========");
                    }
                    break;
                case "2":
                    loop = false;
                    break;
                default:
                    System.out.println("你输入有误请重新输入！");
            }
        }
    }
}
